package com.example.news.helper;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.example.news.bean.UserInfo;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 保存用户信息的管理类
 * Created by libin
 */

public class UserManage {

   private static UserManage instance;

   private UserManage() {
   }

   public static UserManage getInstance() {
      if (instance == null) {
         instance = new UserManage();
      }
      return instance;
   }


   /**
    * 保存自动登录的用户信息
    */
   public void saveUserInfo(Context context, String username, String password,String time,int count) {
      SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);//Context.MODE_PRIVATE表示SharePrefences的数据只有自己应用程序能访问。
      SharedPreferences.Editor editor = sp.edit();
      Date date = new Date();
      SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
      editor.putString("username", username);
      editor.putString("password", password);
      editor.putString("createtime", dateFormat.format(date));
      editor.putInt("readcount", count);
      editor.commit();
   }
   public void saveUserInfo(Context context, String username, String password,String time,int count,String habbit) {
      SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);//Context.MODE_PRIVATE表示SharePrefences的数据只有自己应用程序能访问。
      SharedPreferences.Editor editor = sp.edit();
      Date date = new Date();
      SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
      editor.putString("username", username);
      editor.putString("password", password);
      editor.putString("createtime", dateFormat.format(date));
      editor.putInt("readcount", count);
      editor.putString("habbit", habbit);
      editor.commit();
   }
//   public void savehabbit(Context context,String habbit){
//      SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);//Context.MODE_PRIVATE表示SharePrefences的数据只有自己应用程序能访问。
//      SharedPreferences.Editor editor = sp.edit();
//      editor.putString("habbit", habbit);
//   }

   /**
    * 获取用户信息model
    *
    * @param context
    * @param
    * @param
    */
   public UserInfo getUserInfo(Context context) {
      SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
      UserInfo userInfo = new UserInfo();

      userInfo.setUserName(sp.getString("username", ""));
      userInfo.setPassword(sp.getString("password", ""));
      userInfo.setCreatetime(sp.getString("createtime", ""));
      userInfo.setreadcount(sp.getInt("readcount",0));
      userInfo.setHabbit(sp.getString("habbit", ""));
      return userInfo;
   }


   /**
    * userInfo中是否有数据
    */
   public boolean hasUserInfo(Context context) {
      UserInfo userInfo = getUserInfo(context);
      if (userInfo != null) {
         if ((!TextUtils.isEmpty(userInfo.getUserName())) && (!TextUtils.isEmpty(userInfo.getPassword()))) {//有数据
            return true;
         } else {
            return false;
         }
      }
      return false;
   }
   public void deleteuser(Context context) {
      SharedPreferences sharedPreferences = context.getSharedPreferences("userInfo", context.MODE_PRIVATE);
      SharedPreferences.Editor edit = sharedPreferences.edit();
      edit.remove("username");
      edit.remove("password");
      edit.remove("createtime");
      edit.remove("readcount");
      edit.commit();
   }
}